﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace WebSchedulerDemo.xml {
	public partial class Project : System.Web.UI.Page {
		PlanDataSet ds;
		string fileName;

		PlanDataSet.ProjectsRow row;

		protected void Page_Load(object sender, EventArgs e) {
			Response.Cache.SetCacheability(HttpCacheability.NoCache);

			//
			// Load from XML file
			//
			ds = new PlanDataSet();
			fileName = MapPath("../App_Data/ProjectsPlanner.xml");
			if (File.Exists(fileName))
				ds.ReadXml(fileName);

			int id = int.Parse(Request["id"]);
			row = ds.Projects.FindByProject_ID(id);

			IDLabel.Text = id.ToString();
			if (!IsPostBack) {
				NameText.Text = row.Name;
				DescriptionText.Text = row.Description;
				if (row.IsColorNull())
					ColorDropList.SelectedIndex = 0;
				else
					ColorDropList.SelectedValue = row.Color;
			}
		}

		protected void SaveButton_Click(object sender, EventArgs e) {
			row.Name = NameText.Text;
			row.Description = DescriptionText.Text;
			if (ColorDropList.SelectedIndex==0)
				row.SetColorNull();
			else
				row.Color = ColorDropList.SelectedValue;

			ds.WriteXml(fileName);

			Response.Redirect("default.aspx");
		}
	}
}
